package ar.uba.dc.cmovil.tripplanner.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Date;

import ar.uba.dc.cmovil.tripplanner.R;
import ar.uba.dc.cmovil.tripplanner.domain.Place;
import ar.uba.dc.cmovil.tripplanner.utils.DateFormatter;

/**
 * Created by anton on 10/2/13.
 */
public class PlaceItemWidget extends LinearLayout {

    private Place place;
    private TextView placeName;
    private TextView arrivalDate;
    private TextView otherUsers;
    private PlaceItemEventsHandler eventsHandler;

    public PlaceItemWidget(Context context, Place place) {
        super(context);

        this.place = place;
        this.setOrientation(HORIZONTAL);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.place_widget_view, this, true);

        ImageButton addToCalendar = (ImageButton) getChildAt(0);
        LinearLayout container = (LinearLayout) getChildAt(1);
        this.placeName = (TextView) container.getChildAt(0);
        this.otherUsers = (TextView) container.getChildAt(1);
        this.arrivalDate = (TextView) getChildAt(2);
        Button deleteButton = (Button) getChildAt(3);

        setPlaceName(place.getName());
        if (place.getArrivalDate() != null) {
            setArrivalDate(place.getArrivalDate());
        }

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (eventsHandler != null) {
                    eventsHandler.onPlaceSelected(PlaceItemWidget.this.place);
                }
            }
        });

        deleteButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (eventsHandler != null) {
                    eventsHandler.onPlaceDelete(PlaceItemWidget.this.place);
                }
            }
        });

        addToCalendar.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (eventsHandler != null) {
                    eventsHandler.onAddToCalendarClicked(PlaceItemWidget.this.place);
                }
            }
        });
    }

    private void setPlaceName(String name) {
        placeName.setText(name);
    }

    private void setArrivalDate(Date date) {
        arrivalDate.setText(DateFormatter.formatDate(date));
    }

    public void setEventsHandler(PlaceItemEventsHandler handler) {
        eventsHandler = handler;
    }

    public void setOtherUsers(Integer otherUsers) {
        this.otherUsers.setText("otros usuarios: " + otherUsers);
    }

    public interface PlaceItemEventsHandler {
        void onPlaceDelete(Place place);

        void onPlaceSelected(Place place);

        void onAddToCalendarClicked(Place place);
    }


}
